package com.goktech.controller;

import com.goktech.entity.User;
import com.goktech.service.BorrowService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName adminBorrowController
 * @Description TODO
 * @Author 邱华英
 * @Date 2022/10/8 19:30
 * @Version 1.0
 */

@Controller
@RequestMapping("admin/borrow")
public class adminBorrowController {

    @Autowired
    private BorrowService borrowService;

    @Autowired
    private User user;

    @RequestMapping("/")
    public String UserSelectAll(){
        return "redirect:search/add?yeshu=0";
    }

    @RequestMapping("/search/add")
    public String searchAdd(Integer yeshu, HttpServletRequest request){
        List<Map<String, Object>> byAll = null;
        byAll = borrowService.findByAll(yeshu * 5, 5);

        if(byAll.size() == 0 && yeshu!=0){
            byAll = borrowService.findByAll((yeshu-1)*5,5);
        }else{
            yeshu=yeshu+1;
        }
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("borrows",byAll);

        System.out.println(byAll);
        return "admin/userBorrow";
    }

    @RequestMapping("/search/delete")
    public String searchDelete(Integer yeshu,HttpServletRequest request){
        List<Map<String, Object>> byAll = null;
        if(yeshu>1){
            yeshu=yeshu-1;
        }
        byAll = borrowService.findByAll((yeshu-1)*5,5);
        request.setAttribute("yeshu",yeshu);
        request.setAttribute("borrows",byAll);
        return "admin/userBorrow";
    }

    @PostMapping("/select")
    public String borrowBook(@Param("select") String select, @Param("text") String text, HttpServletRequest request) throws UnsupportedEncodingException {
        List<Map<String, Object>> AllList = null;
        if("zhanghao".equals(select)){
            user.setZhanghao("%"+text.trim()+"%");
            AllList  = borrowService.findByLikeZhanghao(user);
        }else if("username".equals(select)){
            String text1 = new String(text.getBytes("ISO-8859-1"), "utf-8").trim();
            System.out.println("text1:"+text1);
            user.setUserName("%"+text1.trim()+"%");
            AllList = borrowService.findByLikeUserName(user);
        }
        request.setAttribute("borrows",AllList);
        request.setAttribute("yeshu",1);
        return "admin/userBorrow";
    }

}
